/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "shell_port.h"
#include <stdio.h>
#include <stdlib.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
typedef struct pid_def
{
  /* data */
  float P;
  float I;
  float D;
}PID;

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for Helloworld */
osThreadId_t HelloworldHandle;
const osThreadAttr_t Helloworld_attributes = {
  .name = "Helloworld",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for shell_cmd */
osThreadId_t shell_cmdHandle;
const osThreadAttr_t shell_cmd_attributes = {
  .name = "shell_cmd",
  .stack_size = 512 * 4,
  .priority = (osPriority_t) osPriorityLow,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void Hello_Task(void *argument);
void Shell_Task(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of Helloworld */
  HelloworldHandle = osThreadNew(Hello_Task, NULL, &Helloworld_attributes);

  /* creation of shell_cmd */
  shell_cmdHandle = osThreadNew(Shell_Task, NULL, &shell_cmd_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_Hello_Task */
/**
  * @brief  Function implementing the Helloworld thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_Hello_Task */
void Hello_Task(void *argument)
{
  /* USER CODE BEGIN Hello_Task */
  /* Infinite loop */
  printf("This is hello task!\r\n");
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END Hello_Task */
}

/* USER CODE BEGIN Header_Shell_Task */
/**
* @brief Function implementing the shell_cmd thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Shell_Task */
void Shell_Task(void *argument)
{
  /* USER CODE BEGIN Shell_Task */
  User_Shell_Init();
  /* Infinite loop */
  for(;;)
  {
    shellTask(&shell);
  }
  /* USER CODE END Shell_Task */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
PID user_pid = {1.2,1.3,1.4};
int set_pid(PID *new_pid)
{
  user_pid.P = new_pid->P;
  user_pid.I = new_pid->I;
  user_pid.D = new_pid->D;
  printf("new pid: P:%f I:%f D:%f\r\n",user_pid.P,user_pid.I,user_pid.D);
  return 0;
}
SHELL_EXPORT_CMD_AGENCY(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC),set_pid,set_pid,set pid param,&(PID){SHELL_PARAM_FLOAT(p1),SHELL_PARAM_FLOAT(p2),SHELL_PARAM_FLOAT(p3)});


int read_pid(void)
{
  printf("user pid: P:%f I:%f D:%f\r\n",user_pid.P,user_pid.I,user_pid.D);
  return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC), read_pid, read_pid, read pid test);

int list_task(void)
{
  printf("    thread        pri    status        surplus stack  \r\n");
  printf("-------------    ----- ----------     --------------  \r\n");
  //获取当前运行的线程数
  uint32_t array_items = osThreadGetCount();
  osThreadId_t* thread_array = (osThreadId_t*)calloc(array_items,sizeof(osThreadId_t));
  if(thread_array == NULL)
  {
    printf(" %s-%d calloc fail!\r\n",__FILE__,__LINE__);
    return 1;
  }
  //枚举活动线程
  osThreadEnumerate(thread_array,array_items);
  //遍历输出
  osPriority_t  pri;//优先级
  osThreadState_t status; //状态
  //uint32_t stack_size;//堆栈大小
  uint32_t stack_space;//剩余堆栈大小
  for(int i = 0;i<array_items;i++)
  {
    pri = osThreadGetPriority(thread_array[i]);
    status = osThreadGetState(thread_array[i]);
    //stack_size = osThreadGetStackSize(thread_array[i]);
    stack_space = osThreadGetStackSpace(thread_array[i]);


    printf("%-*.*s  %3d  ",configMAX_TASK_NAME_LEN,configMAX_TASK_NAME_LEN,osThreadGetName(thread_array[i]),pri);

    if(status == osThreadInactive)          printf(" Inactive      ");
    else if(status == osThreadReady)        printf(" Ready         ");
    else if(status == osThreadRunning)      printf(" Running       ");
    else if(status == osThreadBlocked)      printf(" Blocked       ");
    else if(status == osThreadTerminated)   printf(" Terminated    ");
    else if(status == osThreadError)        printf(" Error         ");
    else if(status == osThreadReserved)     printf(" optimization  ");
    printf(" 0x%08x \r\n",stack_space);
  }
  printf("\r\n");
  return 0;
}

SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC), ps, list_task, List threads in the system);
/* USER CODE END Application */

